package com.ja.openchat.ui.contact;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ja.openchat.R;
import com.zy.openchat.core.model.circle.BlogInfo;
import com.zy.openchat.core.model.photo.PhotoInfo;
import com.zy.openchat.core.model.video.VideoInfo;
import com.zy.openchat.util.Common;

public class ContactDynamicGridAdapter extends BaseQuickAdapter<BlogInfo, BaseViewHolder> {

    public ContactDynamicGridAdapter() {
        super(R.layout.item_contact_album);
    }

    @Override
    protected void convert(BaseViewHolder helper, BlogInfo blogInfo) {
        ImageView iv_bg = helper.getView(R.id.iv_bg);
        View iv_is_video = helper.getView(R.id.iv_is_video);
        blogInfo.checkMediaDownload(getContext());
        if (blogInfo.isVideoType()) {
            VideoInfo videoInfo = blogInfo.content.video;
            RequestBuilder<Drawable> thumbnailRequest = null;
            if (videoInfo.thumbnail != null && videoInfo.thumbnail.isThumbnailDownloaded()) {
                thumbnailRequest = Glide
                        .with(getContext())
                        .load(videoInfo.thumbnail.localPath());
            } else {
                thumbnailRequest = Glide
                        .with(getContext())
                        .load(R.drawable.image_chat_placeholder_white);
            }
            Glide.with(getContext())
                    .load(videoInfo.localVideoPath())
                    .thumbnail(thumbnailRequest)
                    .into(iv_bg);
            iv_is_video.setVisibility(View.VISIBLE);
        }else if(!Common.isEmpty(blogInfo.content.photos)){
            PhotoInfo photoInfo =  blogInfo.content.photos.get(0);
            if(photoInfo.isPhotoDownloaded()){
                Glide.with(getContext())
                        .load(photoInfo.getPhotoImagePath())
                        .into(iv_bg);
            }else{
                iv_bg.setImageResource(R.drawable.image_chat_placeholder_white);
            }
            iv_is_video.setVisibility(View.GONE);
        }
    }
}
